package com.techfansy.pickerTimer.Timer.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;


import com.example.alltoollib.R;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/15.
 */

public abstract class BottomDialog {
    private static Dialog bottonDialog;
    private static AlertDialog dialog;
    private static Intent intent;

    private static Map<Integer, Dialog> map = new HashMap<>();

    public static void Alldismiss() {

        map.clear();
    }

    //底部弹出
    public static void create(Context context, View layout) {
        bottonDialog = new Dialog(context, R.style.BottomDialog);
        bottonDialog.setContentView(layout);
        ViewGroup.LayoutParams lp = layout.getLayoutParams();
        lp.width = context.getResources().getDisplayMetrics().widthPixels;
        layout.setLayoutParams(lp);
        bottonDialog.getWindow().setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        bottonDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);
        bottonDialog.show();
    }
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
    //中间位置弹出标记第几个位置
    public static void createCenterPosition(Context context, View layout, int position) {
        bottonDialog = new Dialog(context, R.style.BottomDialog);

        bottonDialog.setContentView(layout);
        ViewGroup.LayoutParams lp = layout.getLayoutParams();

        if (Build.VERSION.SDK_INT<19){
            lp.width = 400;

        }else  if (Build.VERSION.SDK_INT<=21){
            lp.width = 600;
        }
        else  if (Build.VERSION.SDK_INT<=23){
            lp.width = 800;
        }else {
            lp.width = 800;
        }



        layout.setLayoutParams(lp);
        bottonDialog.getWindow().setGravity(Gravity.CENTER);
        bottonDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);
        bottonDialog.show();
        map.put(position, bottonDialog);
    }

    //中间位置弹出弹出
    public static void createCenter(Context context, View layout) {
        bottonDialog = new Dialog(context, R.style.BottomDialog);

        bottonDialog.setContentView(layout);
        ViewGroup.LayoutParams lp = layout.getLayoutParams();
        if (Build.VERSION.SDK_INT<19){
            lp.width = 400;
        }else  if (Build.VERSION.SDK_INT<=23){
            lp.width = 800;
        }else {
            lp.width = 800;
        }
        layout.setLayoutParams(lp);
        bottonDialog.getWindow().setGravity(Gravity.CENTER);
        bottonDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);
        bottonDialog.show();
    }

    public static void Dismiss() {
        if (bottonDialog.isShowing())
            bottonDialog.dismiss();

    }

    public static boolean isEmtory(int position) {
        if (map.get(position) != null) {
            return true;
        }
        return false;
    }

    public static boolean isShowPosition(int position) {
        if (((Dialog) map.get(position)) != null) {
            return !map.get(position).isShowing();
        } else {
            return false;
        }
    }

    public static void dissmiss(int position) {
        map.get(position).cancel();
        map.get(position).dismiss();
    }

    public static void showPosition(int position) {
        map.get(position).show();
    }

    public static void dismissPosition(int position) {
        if (map.get(position).isShowing())
            map.get(position).hide();

    }



    public static void show() {
        bottonDialog.show();
    }

}
